package com.itlong.cloud.commons.service.impl;

import com.itlong.cloud.POJO.DTO.common.LimitedDeviceDto;
import com.itlong.cloud.POJO.DTO.property.CloudIntercomChangeLogGetDTO;
import com.itlong.cloud.POJO.PO.*;
import com.itlong.cloud.POJO.VO.common.LimitedDeviceVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.commons.dao.*;
import com.itlong.cloud.commons.service.ICloudDeviceBaseService;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.enumerate.SystemTypeEnum;
import com.itlong.cloud.operate.dao.IDeviceRunLogDao;
import com.itlong.cloud.property.dao.IDeviceCloudControllerDao;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.encrypt.UserTokenUtil;
import com.itlong.cloud.utils.random.IdWorker;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * <desc>
 *      设备业务接口实现类
 * </desc>
 *
 * @create 2018/03/28
 */
@Service
public class CloudDeviceBaseServiceImpl implements ICloudDeviceBaseService {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(CloudDeviceBaseServiceImpl.class);
    @Autowired
    private ICloudElevatorDao iCloudElevatorDao;

    @Autowired
    private ICloudEntranceDao iCloudEntranceDao;

    @Autowired
    private IDevicePadInfoDao iDevicePadInfoDao;

    @Autowired
    private ISpecialDeviceElevatorInfoDao specialDeviceElevatorInfoDao;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ISpecialDeviceCloudControllerInfoDao iSpecialDeviceCloudControllerInfoDao;

    @Autowired
    private ISpecialDeviceEntranceInfoDao iSpecialDeviceEntranceInfoDao;

    @Autowired
    private ISpecialDevicePadInfoDao iSpecialDevicePadInfoDao;

    @Autowired
    private IDeviceOnlineStatusAlterLogDao iDeviceOnlineStatusAlterLogDao;

    @Autowired
    private IDeviceRunLogDao iDeviceRunLogDao;

    @Autowired
    IDeviceCloudControllerDao iDeviceCloudControllerDao;


    final static IdWorker idWorker = new IdWorker(18);

    @Value("${" + PlatformConstants.CLOUD_ELEVATOR_TOKEN_KEY + "}")
    String cloudElevatorTokenKey;


    @Value("${" + PlatformConstants.CLOUD_ENTRANCE_TOKEN_KEY + "}")
    String cloudEntranceTokenKey;

    @Value("${" + PlatformConstants.CLOUD_CONTROLLER_TOKEN_KEY + "}")
    String cloudControllerTokenKey;


    /**
     * <desc>
     *  保存受限设备和缓存设备最新更新时间
     * </desc>
     *
     * @param getDTO
     * @return
     * @author caoshuai
     * @createDate 2020/11/20 18:07
     */
    @Override
    public LimitedDeviceVO saveLimitedDevice(LimitedDeviceDto getDTO) throws UnsupportedEncodingException {

        LimitedDeviceVO limitedDeviceVO = new LimitedDeviceVO();
        limitedDeviceVO.setIsLimitedDevice(false);
        if(StringUtils.isBlank(getDTO.getDeviceType())){
            return limitedDeviceVO;
        }

        // 门禁实体
        DeviceEntranceInfoPO deviceEntranceInfoPO ;
        // 门禁集合
        List<DeviceEntranceInfoPO> deviceEntranceInfoPOS = new ArrayList<>();
        // 门禁设备ID
        List<String> deviceListBySnAndDoorCount;

        switch (PropertyEquipTypeEnum.getByEquipType(Integer.valueOf(getDTO.getDeviceType()))) {
            //电梯
            case CLOUD_ELEVATOR_DEVICE_TYPE:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getElevatorDeviceUniqueBySn(getDTO.getSn()), System.currentTimeMillis() + "");
                //获取电梯设备信息
                Map<String, Object> param = new HashMap<>();
                param.put("sn", getDTO.getSn());
                param.put("elevatorType", PropertyEquipTypeEnum.BLUETOOTH_ELEVATOR.getType());

                DeviceElevatorInfoPO deviceElevatorInfoPO = iCloudElevatorDao.getElevatorInfoBySn(param);
                // 不存在为受限设备 否则为正常设备
                if(null == deviceElevatorInfoPO) {

                    limitedDeviceVO.setDeviceUnique(StringHandlerUtil.getElevatorDeviceUniqueBySn(getDTO.getSn()));
                    limitedDeviceVO.setIsLimitedDevice(true);
                    //设置token
                    limitedDeviceVO.setToken(UserTokenUtil.tokenEncrypt(getDTO.getSn(),cloudElevatorTokenKey, SystemTypeEnum.CLOUD_ELEVATOR_SERVER.getType()));
                    //设置过期时间
                    limitedDeviceVO.setInvalidTime(DateUtil.fomatDate(
                            DateUtils.addSeconds(new Date(), RedisConstant.CLOUD_ELEVATOR_LOGIN_TIME.intValue()),
                            "yyyy-MM-dd HH:mm:ss"
                    ));
                    limitedDeviceVO.setEncryptMethod(StringUtils.isNotBlank(getDTO.getEncryptionAbility()) ? Integer.valueOf(getDTO.getEncryptionAbility()) : 0);
                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.REDIS_IOT_LIMITED_CLOUD_ELEVATOR_STATE_CACHE + StringHandlerUtil.getElevatorDeviceUniqueBySn(getDTO.getSn()),
                            RedisConstant.REDIS_IOT_LIMITED_CLOUD_ELEVATOR_STATE_CACHE_TIME,"1");

                    //保存特殊电梯设备信息
                    SpecialDeviceElevatorInfoPO specialDeviceElevatorInfoPO = new SpecialDeviceElevatorInfoPO();
                    specialDeviceElevatorInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDeviceElevatorInfoPO.setSn(getDTO.getSn());
                    specialDeviceElevatorInfoPO.setDeviceUnique(StringHandlerUtil.getElevatorDeviceUniqueBySn(getDTO.getSn()));
                    specialDeviceElevatorInfoPO.setStatus(1);
                    specialDeviceElevatorInfoPO.setCreateTime(new Date());
                    Integer row = specialDeviceElevatorInfoDao.insert(specialDeviceElevatorInfoPO);
                    }
                    //设置缓存
                    redisService.set(RedisConstant.CLOUD_ELEVATOR_LOGIN_REDIS_KEY + getDTO.getSn(),
                            RedisConstant.CLOUD_ELEVATOR_LOGIN_TIME, limitedDeviceVO);
                    //清空过期的轮询命令
                    //iCloudElevatorDao.clearExpiredChangeLog(StringHandlerUtil.getProjectIdBySn(sn) + "1" +  StringHandlerUtil.getDeviceIdBySn(sn));



                break;
            //群控
            case CLOUD_ELEVATOR_CLUSTER_CONTROLLER:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getControllerDeviceUniqueBySn(getDTO.getSn()), System.currentTimeMillis() + "");
                //获取群控器设备信息
                DeviceCloudControllerInfoPO deviceCloudControllerInfoPO = iCloudElevatorDao.getControllerInfoBySn(getDTO.getSn());
                if(null == deviceCloudControllerInfoPO) {
                    limitedDeviceVO.setDeviceUnique(StringHandlerUtil.getControllerDeviceUniqueBySn(getDTO.getSn()));
                    limitedDeviceVO.setIsLimitedDevice(true);
                    //设置token
                    limitedDeviceVO.setToken(UserTokenUtil.tokenEncrypt(getDTO.getSn(), cloudControllerTokenKey, SystemTypeEnum.CLOUD_ELEVATOR_SERVER.getType()));
                    //设置过期时间
                    limitedDeviceVO.setInvalidTime(DateUtil.fomatDate(
                            DateUtils.addSeconds(new Date(), RedisConstant.CLOUD_CONTROLLER_LOGIN_TIME.intValue()),
                            "yyyy-MM-dd HH:mm:ss"
                    ));
                    limitedDeviceVO.setEncryptMethod(StringUtils.isNotBlank(getDTO.getEncryptionAbility()) ? Integer.valueOf(getDTO.getEncryptionAbility()) : 0);
                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.DEVICE_LIMITED_CLUSTER_IS_ONLINE_KEY + StringHandlerUtil.getControllerDeviceUniqueBySn(getDTO.getSn()),
                            RedisConstant.DEVICE_LIMITED_CLUSTER_IS_ONLINE_CACHE_TIME,"1");
                    SpecialDeviceCloudControllerInfoPO specialDeviceCloudControllerInfoPO = new SpecialDeviceCloudControllerInfoPO();
                    specialDeviceCloudControllerInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDeviceCloudControllerInfoPO.setSn(getDTO.getSn());
                    specialDeviceCloudControllerInfoPO.setDeviceUnique(StringHandlerUtil.getControllerDeviceUniqueBySn(getDTO.getSn()));
                    specialDeviceCloudControllerInfoPO.setStatus(1);
                    specialDeviceCloudControllerInfoPO.setCreateTime(new Date());
                    iSpecialDeviceCloudControllerInfoDao.insert(specialDeviceCloudControllerInfoPO);
                    //设置缓存
                    redisService.set(RedisConstant.CLOUD_CONTROLLER_LOGIN_REDIS_KEY + getDTO.getSn(),
                            RedisConstant.CLOUD_CONTROLLER_LOGIN_TIME, limitedDeviceVO);
                }

                break;
            case CLOUD_ENTRANCE:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()), System.currentTimeMillis() + "");
//                deviceEntranceInfoPO = iCloudEntranceDao.getEntranceInfoByDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                deviceListBySnAndDoorCount = getDeviceListBySnAndDoorCount(getDTO.getSn(), getDTO.getDoorCount());

                for(String deviceUnique : deviceListBySnAndDoorCount){
                    DeviceEntranceInfoPO entranceInfoByDeviceUnique = iCloudEntranceDao.getEntranceInfoByDeviceUnique(deviceUnique);
                    if(null != entranceInfoByDeviceUnique) {
                        deviceEntranceInfoPOS.add(entranceInfoByDeviceUnique);
                    }
                }

                if(deviceEntranceInfoPOS.size() == 0) {
                    limitedDeviceVO.setIsLimitedDevice(true);
                    //设置token
                    limitedDeviceVO.setToken(UserTokenUtil.tokenEncrypt(String.format("%s%s%s",getDTO.getSn(),"|", DateUtil.fomatDate(new Date(),DateUtil.DATE_FORMAT_TYPE2)),
                            cloudEntranceTokenKey, SystemTypeEnum.CLOUD_ELEVATOR_SERVER.getType()));

                    limitedDeviceVO.setEncryptMethod(getDTO.getEncryptionAbility() != null ? Integer.valueOf(getDTO.getEncryptionAbility()) : 0);
                    limitedDeviceVO.setInvalidTime(DateUtil.fomatDate(
                            DateUtils.addSeconds(new Date(), RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME.intValue()),
                            "yyyy-MM-dd HH:mm:ss"
                    ));
                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.DEVICE_LIMITED_ENTRANCE_IS_ONLINE_KEY + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()),
                            RedisConstant.DEVICE_LIMITED_ENTRANCE_IS_ONLINE_CACHE_TIME,"1");
                    SpecialDeviceEntranceInfoPO specialDeviceEntranceInfoPO = new SpecialDeviceEntranceInfoPO();
                    specialDeviceEntranceInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDeviceEntranceInfoPO.setSn(getDTO.getSn());
                    specialDeviceEntranceInfoPO.setDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                    specialDeviceEntranceInfoPO.setStatus(1);
                    specialDeviceEntranceInfoPO.setEntranceCategory(22);
                    specialDeviceEntranceInfoPO.setCreateTime(new Date());
                    iSpecialDeviceEntranceInfoDao.insert(specialDeviceEntranceInfoPO);
                    //设置缓存
                    redisService.set(RedisConstant.CLOUD_ENTRANCE_LOGIN_REDIS_KEY + getDTO.getSn(),
                            RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME, limitedDeviceVO);
                }
                break;
            case CLOUD_ENTRANCE_DEVICE:
            case CLOUD_WIPPEN_DEVICE:
            case CLOUD_WIPPEN_DEVICE_TYPE:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()), System.currentTimeMillis() + "");
//                deviceEntranceInfoPO = iCloudEntranceDao.getEntranceInfoByDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                deviceListBySnAndDoorCount = getDeviceListBySnAndDoorCount(getDTO.getSn(), getDTO.getDoorCount());

                for(String deviceUnique : deviceListBySnAndDoorCount){
                    DeviceEntranceInfoPO entranceInfoByDeviceUnique = iCloudEntranceDao.getEntranceInfoByDeviceUnique(deviceUnique);
                    if(null != entranceInfoByDeviceUnique) {
                        deviceEntranceInfoPOS.add(entranceInfoByDeviceUnique);
                    }
                }

                if(deviceEntranceInfoPOS.size() == 0) {
                    limitedDeviceVO.setIsLimitedDevice(true);
                    //设置token
                    limitedDeviceVO.setToken(UserTokenUtil.tokenEncrypt(String.format("%s%s%s",getDTO.getSn(),"|", DateUtil.fomatDate(new Date(),DateUtil.DATE_FORMAT_TYPE2)),
                            cloudEntranceTokenKey, SystemTypeEnum.CLOUD_ELEVATOR_SERVER.getType()));
                    limitedDeviceVO.setEncryptMethod(getDTO.getEncryptionAbility() != null ? Integer.valueOf(getDTO.getEncryptionAbility()) : 0);
                    limitedDeviceVO.setInvalidTime(DateUtil.fomatDate(
                            DateUtils.addSeconds(new Date(), RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME.intValue()),
                            "yyyy-MM-dd HH:mm:ss"
                    ));

                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.DEVICE_LIMITED_WIPPEN_IS_ONLINE_KEY + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()),
                            RedisConstant.DEVICE_LIMITED_WIPPEN_IS_ONLINE_CACHE_TIME,"1");

                    SpecialDeviceEntranceInfoPO specialDeviceEntranceInfoPO = new SpecialDeviceEntranceInfoPO();
                    specialDeviceEntranceInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDeviceEntranceInfoPO.setSn(getDTO.getSn());
                    specialDeviceEntranceInfoPO.setDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                    specialDeviceEntranceInfoPO.setStatus(1);
                    specialDeviceEntranceInfoPO.setEntranceCategory(23);
                    specialDeviceEntranceInfoPO.setCreateTime(new Date());
                    iSpecialDeviceEntranceInfoDao.insert(specialDeviceEntranceInfoPO);
                    //设置缓存
                    redisService.set(RedisConstant.CLOUD_ENTRANCE_LOGIN_REDIS_KEY + getDTO.getSn(),
                            RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME, limitedDeviceVO);
                }
                break;
            case CLOUD_UNIBODY_FACE_DEVICE:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()), System.currentTimeMillis() + "");
                if("2".equals(getDTO.getDeviceUseEnvironment())) {
//                    deviceEntranceInfoPO = iCloudEntranceDao.getEntranceInfoByDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
//                    if(null == deviceEntranceInfoPO) {
                    limitedDeviceVO.setIsLimitedDevice(true);

                    //设置token
                    limitedDeviceVO.setToken(UserTokenUtil.tokenEncrypt(String.format("%s%s%s",getDTO.getSn(),"|", DateUtil.fomatDate(new Date(),DateUtil.DATE_FORMAT_TYPE2)),
                            cloudEntranceTokenKey, SystemTypeEnum.CLOUD_ELEVATOR_SERVER.getType()));
                    limitedDeviceVO.setEncryptMethod(getDTO.getEncryptionAbility() != null ? Integer.valueOf(getDTO.getEncryptionAbility()) : 0);
                    limitedDeviceVO.setInvalidTime(DateUtil.fomatDate(
                            DateUtils.addSeconds(new Date(), RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME.intValue()),
                            "yyyy-MM-dd HH:mm:ss"
                    ));

                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.DEVICE_LIMITED_UNIBODY_FACE_IS_ONLINE_KEY + StringHandlerUtil.getElevatorDeviceUniqueBySn(getDTO.getSn()),
                            RedisConstant.DEVICE_LIMITED_UNIBODY_FACE_IS_ONLINE_CACHE_TIME,"1");

                    SpecialDeviceEntranceInfoPO specialDeviceEntranceInfoPO = new SpecialDeviceEntranceInfoPO();
                    specialDeviceEntranceInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDeviceEntranceInfoPO.setSn(getDTO.getSn());
                    specialDeviceEntranceInfoPO.setDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                    specialDeviceEntranceInfoPO.setStatus(1);
                    specialDeviceEntranceInfoPO.setEntranceCategory(24);
                    specialDeviceEntranceInfoPO.setCreateTime(new Date());
                    iSpecialDeviceEntranceInfoDao.insert(specialDeviceEntranceInfoPO);
                    //设置缓存
                    redisService.set(RedisConstant.CLOUD_ENTRANCE_LOGIN_REDIS_KEY + getDTO.getSn(),
                            RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME, limitedDeviceVO);
//                    }
                }

                break;

            case PAD_FACE_RECOGNITION:
            case PAD:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getProjectIdBySn(getDTO.getSn()) + "4" + StringHandlerUtil.getDeviceIdBySn(getDTO.getSn()), System.currentTimeMillis() + "");
                DevicePadInfoPO devicePadInfoPO = iDevicePadInfoDao.getPadInfoByDeviceUnique(StringHandlerUtil.getProjectIdBySn(getDTO.getSn()) + "4" + StringHandlerUtil.getDeviceIdBySn(getDTO.getSn()));
                if(null == devicePadInfoPO){
                    limitedDeviceVO.setIsLimitedDevice(true);
                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.DEVICE_LIMITED_PAD_IS_ONLINE_KEY + StringHandlerUtil.getProjectIdBySn(getDTO.getSn()) + "4" + StringHandlerUtil.getDeviceIdBySn(getDTO.getSn()),
                            RedisConstant.DEVICE_LIMITED_PAD_IS_ONLINE_CACHE_TIME,"1");

                    SpecialDevicePadInfoPO specialDevicePadInfoPO = new SpecialDevicePadInfoPO();
                    specialDevicePadInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDevicePadInfoPO.setSn(getDTO.getSn());
                    specialDevicePadInfoPO.setDeviceUnique(StringHandlerUtil.getProjectIdBySn(getDTO.getSn()) + "4" + StringHandlerUtil.getDeviceIdBySn(getDTO.getSn()));
                    specialDevicePadInfoPO.setStatus(1);
                    specialDevicePadInfoPO.setCreateTime(new Date());
                    iSpecialDevicePadInfoDao.insert(specialDevicePadInfoPO);
                }
                break;
            case HARDWARE_CLOUD_UNIBODY_ENTRANCE_DEVICE:
                //缓存设备最新时间
                redisService.set(RedisConstant.DEVICE_NEW_UPDATE_CACHE_TIME + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()), System.currentTimeMillis() + "");
//                deviceEntranceInfoPO = iCloudEntranceDao.getEntranceInfoByDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                DeviceEntranceInfoPO entranceInfoByDeviceUnique = iCloudEntranceDao.getEntranceInfoByDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));

                if(null == entranceInfoByDeviceUnique) {
                    limitedDeviceVO.setIsLimitedDevice(true);
                    //设置token
                    limitedDeviceVO.setToken(UserTokenUtil.tokenEncrypt(String.format("%s%s%s",getDTO.getSn(),"|", DateUtil.fomatDate(new Date(),DateUtil.DATE_FORMAT_TYPE2)),
                            cloudEntranceTokenKey, SystemTypeEnum.CLOUD_ELEVATOR_SERVER.getType()));
                    limitedDeviceVO.setEncryptMethod(getDTO.getEncryptionAbility() != null ? Integer.valueOf(getDTO.getEncryptionAbility()) : 0);
                    limitedDeviceVO.setInvalidTime(DateUtil.fomatDate(
                            DateUtils.addSeconds(new Date(), RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME.intValue()),
                            "yyyy-MM-dd HH:mm:ss"
                    ));

                    //在线状态保存 超时时间设置五分钟
                    redisService.set(RedisConstant.DEVICE_LIMITED_WIPPEN_IS_ONLINE_KEY + StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()),
                            RedisConstant.DEVICE_LIMITED_WIPPEN_IS_ONLINE_CACHE_TIME,"1");

                    SpecialDeviceEntranceInfoPO specialDeviceEntranceInfoPO = new SpecialDeviceEntranceInfoPO();
                    specialDeviceEntranceInfoPO.setSpecialDeviceId(idWorker.nextId() + "");
                    specialDeviceEntranceInfoPO.setSn(getDTO.getSn());
                    specialDeviceEntranceInfoPO.setDeviceUnique(StringHandlerUtil.getEntranceDeviceUniqueBySn(getDTO.getSn()));
                    specialDeviceEntranceInfoPO.setStatus(1);
                    specialDeviceEntranceInfoPO.setEntranceCategory(25);
                    specialDeviceEntranceInfoPO.setCreateTime(new Date());
                    iSpecialDeviceEntranceInfoDao.insert(specialDeviceEntranceInfoPO);
                    //设置缓存
                    redisService.set(RedisConstant.CLOUD_ENTRANCE_LOGIN_REDIS_KEY + getDTO.getSn(),
                            RedisConstant.CLOUD_ENTRANCE_LOGIN_TIME, limitedDeviceVO);
                }
                break;

            default:
                break;
        }
        return limitedDeviceVO;
    }

    /**
     * <desc>
     *      通过sn及门的数量获取控制器管控的门禁设备序列号集合
     * </desc>
     *
     * @param sn
     * @param doorCount
     * @return
     * @author Jiaqi.X
     * @createDate 2019/02/22
     */
    private List<String> getDeviceListBySnAndDoorCount(String sn, Integer doorCount){
        List<String> devices = new ArrayList<>();
        Integer deviceId= Integer.valueOf(sn.substring(sn.length() - 4, sn.length()));
        for(int i = deviceId ; i < deviceId+doorCount ; i++ ){
            devices.add(String.format("%s%s%s", StringHandlerUtil.getProjectIdBySn(sn),"2",StringHandlerUtil.autlSupply(4,i+"","0")));
        }
        return devices;
    }

    /**
     * <desc>
     *      保存设备在线状态变更
     * </desc>
     *
     * @param deviceOnlineStatusAlterLogPO
     * @author caoshuai
     * @createDate 2020/11/24 15:29
     */
    @Override
    public void saveDeviceOnlineStatusAlterLog(DeviceOnlineStatusAlterLogPO deviceOnlineStatusAlterLogPO) {
        /*
        String oldStatus = iDeviceOnlineStatusAlterLogDao.getNewTimeDeviceStatus(deviceOnlineStatusAlterLogPO.getDeviceUnique());
        if(StringUtils.isNotBlank(oldStatus) && oldStatus.equals(deviceOnlineStatusAlterLogPO.getOnlineStatus() + "")) {
            return;
        }
        try {
            deviceOnlineStatusAlterLogPO.setStatus(1);
            deviceOnlineStatusAlterLogPO.setCreateTime(new Date());
            Map<String, Object> params = SqlUtil.durableData(deviceOnlineStatusAlterLogPO, PlatformConstants.TABLE_SAVE);
            iDeviceOnlineStatusAlterLogDao.save(params);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
         */
    }

    /**
     * <desc>
     * 保存设备日志信息
     * </desc>
     *
     * @param deviceRunLogPO
     * @author caoshuai
     * @createDate 2020/12/3 14:17
     */
    @Override
    public Integer saveDeviceRunLog(DeviceRunLogPO deviceRunLogPO) throws Exception {
        Map<String, Object> params = SqlUtil.durableData(deviceRunLogPO, PlatformConstants.TABLE_SAVE);

        return iDeviceRunLogDao.save(params);
    }

    /**
     * <desc>
     * 修改设备运行日志上传状态
     * </desc>
     *
     * @param deviceRunLogPO
     * @return
     * @author caoshuai
     * @createDate 2020/12/3 14:17
     */
    @Override
    public Integer updateDeviceRunLog(DeviceRunLogPO deviceRunLogPO) {
        return iDeviceRunLogDao.update(deviceRunLogPO);
    }

    /**
     * <desc>
     * 获取受限getDTO 获取受限设备总数
     * </desc>
     *
     * @param getDTO
     * @return
     * @author caoshuai
     * @createDate 2020/12/3 14:17
     */
    @Override
    public Integer getLimitDeviceTotalByDTO(CloudIntercomChangeLogGetDTO getDTO) {

        return iDeviceRunLogDao.getLimitDeviceTotalByDTO(getDTO);
    }

    /**
     * <desc>
     * 获取平板受限设备总数
     * </desc>
     *
     * @param deviceUnique
     * @return
     * @author caoshuai
     * @createDate 2020/12/3 14:17
     */
    @Override
    public Integer getLimitDevicePadTotalBy(String deviceUnique) {
        return iDeviceRunLogDao.getLimitDevicePadTotalBy(deviceUnique);
    }

}
